﻿// ------------------------------------------------------------------------------------------------
//  <copyright file="Phrase.cs" company="Iveely">
//    Copyright (c) Iveely Liu.  All rights reserved.
//  </copyright>
//  
//  <Create Time>
//    03/02/2013 21:59 
//  </Create Time>
//  
//  <contact owner>
//    liufanping@iveely.com 
//  </contact owner>
//  -----------------------------------------------------------------------------------------------

#region

using System;
using System.Collections;
using System.Collections.Generic;

#endregion

namespace IveelySE.NLP.Common.DataStructure
{
    /// <summary>
    ///   字词语
    /// </summary>
    internal class Word
    {
        #region 字段 OR 属性

        /// <summary>
        ///   字词名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        ///   字词类型(名词，动词，形容词...)
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        ///   义原集
        /// </summary>
        public List<string> Sememes { get; set; }

        #endregion

        #region 构造方法

        public Word()
        {
            this.Sememes = new List<string>();
        }

        /// <summary>
        ///   按照分隔符在句子中加载
        /// </summary>
        /// <param name="text"> 句子文本 demo:英国 ADJ aValue|属性值,attachment|归属,#country|国家,ProperName|专,(UK|英国) </param>
        /// <param name="delimiter"> 分隔符 </param>
        public Word(string text, string delimiter)
        {
            this.Sememes = new List<string>();
            //切分为3个单元
            string[] content = text.Split(new[] {delimiter}, StringSplitOptions.RemoveEmptyEntries);
            if(content.Length != 3)
            {
                return;
            }
            //第一单元是名字
            this.Name = content[0];
            //第二单元是类型
            this.Type = content[1].Replace("\t", "");
            //第三单元是义原组合
            string[] sememe = content[2].Split(',');
            //义原的再次切分
            foreach(string str in sememe)
            {
                string[] temp = str.Split('|');
                if(temp.Length == 2)
                {
                    this.Sememes.Add(temp[1].Replace(")", ""));
                }
            }
        }

        #endregion

        #region 测试

        public static void Test()
        {
            string text = "英国                	N    	place|地方,country|国家,ProperName|专,(Europe|欧洲)";
            var word = new Word(text, "  \t");
        }

        #endregion
    }

    /// <summary>
    ///   字词组
    /// </summary>
    public class Phrase
    {
        #region 属性 OR 字段

        /// <summary>
        ///   字词组存放库
        /// </summary>
        private Hashtable table { get; set; }

        #endregion

        #region 公有方法

        public Phrase()
        {
            this.table = new Hashtable();
        }

        /// <summary>
        ///   添加字词组
        /// </summary>
        /// <param name="text"> 句子文本 demo:英国 ADJ aValue|属性值,attachment|归属,#country|国家,ProperName|专,(UK|英国) </param>
        /// <param name="delimiter"> 分隔符 </param>
        public void Add(string text, string delimiter)
        {
            var word = new Word(text, delimiter);
            if(word.Name != null)
            {
                if(this.table.ContainsKey(word.Name))
                {
                    var temp = (Word) this.table[word.Name];
                    if(word.Sememes.Count < temp.Sememes.Count)
                    {
                        return;
                    }
                    this.table.Remove(word.Name);
                }
                this.table.Add(word.Name, word);
            }
        }

        /// <summary>
        ///   获取关键字对应的义原集合
        /// </summary>
        /// <param name="keyword"> 关键字 </param>
        /// <returns> 返回义原集合 </returns>
        public List<string> GetSememes(string keyword)
        {
            if(this.table.ContainsKey(keyword))
            {
                return ((Word) this.table[keyword]).Sememes;
            }
            return null;
        }

        #endregion
    }
}